//*************************************************************************************
//* 作    者： 
//* 创建时间： 2021-10-11 05:11:36
//*  描    述：

//* ************************************************************************************
using UnityEngine;
using UFrame.Audio;

public class FinderAudioBehaviour : MonoBehaviour
{
    protected AudioContext m_rotateContext;
    protected AudioContext m_moveContext;
    protected float minSpeed = 1f;       //The minimum speed required for audio
    protected float maxSpeed = 30f;      //The speed required for maximum audio
    protected float minVolume = 0f;      //Minimum volume
    protected float maxVolume = 0.6f;     //Maximum volume
    protected float minPitch = .4f;      //Minimum pitch
    protected float maxPitch = 0.8f;      //Maximum pitch
    protected float smoothing = 10;
    protected float m_moveRetio = 10;
    private float m_currentRotateSpeed;
    private float m_currentMoveSpeed;
    private float m_lastAngle;
    private float m_lastPos;
    protected bool m_rotatePlaying;
    protected bool m_movePlaying;
    public bool left;
    private void OnEnable()
    {
        m_rotatePlaying = false;
        m_movePlaying = false;
        m_lastAngle = transform.localEulerAngles.y;
        m_lastPos = transform.position.y;
    }

    private void Start()
    {
        if(left)
        {
            m_rotateContext = SoundAgent.PlayBgm1(404, 0,1);
            m_moveContext = SoundAgent.PlayBgm2(405, 0, 1);
        }
        else
        {
            m_rotateContext = SoundAgent.PlayBgm3(404, 0, 1);
            m_moveContext = SoundAgent.PlayBgm4(405, 0, 1);
        }
    }

    private void Update()
    {
        if (m_rotateContext == null || m_moveContext == null || !m_rotateContext.audioSource || !m_moveContext.audioSource)
        {
            Debug.LogError("finder audio audio play failed!");
            enabled = false;
            return;
        }
        var distance = Mathf.Abs(transform.localEulerAngles.y - m_lastAngle);
        m_lastAngle = transform.localEulerAngles.y;
        m_currentRotateSpeed = Mathf.Lerp(m_currentRotateSpeed, distance / Time.deltaTime, smoothing * Time.deltaTime);
        RefreshAudio(m_rotateContext.audioSource, m_currentRotateSpeed, ref m_rotatePlaying);

        distance = Mathf.Abs(transform.position.y - m_lastPos) * m_moveRetio;
        m_lastPos = transform.position.y;
        m_currentMoveSpeed = Mathf.Lerp(m_currentMoveSpeed, distance / Time.deltaTime, smoothing * Time.deltaTime);
        RefreshAudio(m_moveContext.audioSource, m_currentMoveSpeed, ref m_movePlaying);
    }

    private void OnDisable()
    {
        if(m_rotatePlaying && m_rotateContext?.audioSource)
        {
            m_rotateContext.audioSource.volume = 0;
            m_rotateContext.audioSource.Pause();
            m_rotatePlaying = false;
        }
        if (m_movePlaying && m_moveContext?.audioSource)
        {
            m_moveContext.audioSource.volume = 0;
            m_moveContext.audioSource.Pause();
            m_movePlaying = false;
        }
    }

    private void OnDestroy()
    {
        if (m_rotateContext)
            m_rotateContext.Stop();
    }

    private void RefreshAudio(AudioSource swordAudio, float speed, ref bool playing)
    {
        //If the volume is below a very small amount, apply it
        if (speed > minSpeed)
        {
            //Calculate the percentage the current speed is between the minimum and maximum
            float percentage = Mathf.Clamp((speed - minSpeed) / (maxSpeed - minSpeed), 0f, 1f);

            //Calculate the desired volume and pitch based on the speed percentage
            float targetPitch = Mathf.Lerp(minPitch, maxPitch, percentage);
            float targetVolume = Mathf.Lerp(minVolume, maxVolume, percentage);

            swordAudio.pitch = targetPitch;
            swordAudio.volume = targetVolume;
            if (!playing)
            {
                swordAudio.UnPause();
                playing = true;
            }
        }
        //...otherwise set the values to their minimum and set the audio to inactive
        else if (playing)
        {
            swordAudio.pitch = minPitch;
            swordAudio.volume = minVolume;
            playing = false;
            swordAudio.Pause();
            swordAudio.time = 0;
        }
    }
}
